package org.bluewhale.update;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.bluewhale.hibernate.dao.*;

import com.planetj.taste.impl.model.file.FileDataModel;
import com.planetj.taste.impl.recommender.CachingRecommender;
import com.planetj.taste.impl.recommender.slopeone.SlopeOneRecommender;
import com.planetj.taste.model.DataModel;
import com.planetj.taste.model.User;
import com.planetj.taste.recommender.RecommendedItem;
import com.planetj.taste.recommender.Recommender;


public class DbDataUpdater {
	public static void main(String[] args) throws Exception {
		DataModel model = new FileDataModel(new File("data.txt"));

		// Make a weighted slope one recommender
		Recommender recommender = new CachingRecommender(new SlopeOneRecommender(model));
		// List<RecommendedItem> recommendations = recommender.recommend("1234", 10);
		Iterator<? extends User> users = model.getUsers().iterator();
		
		while (users.hasNext()) {
			User user = users.next();
			List<RecommendedItem> recommendations = recommender.recommend(user.getID(), 6);
			// compose a recommended item
			StringBuffer bookIdList = new StringBuffer();
			for (int i = 0; i < recommendations.size(); i ++) {
				bookIdList.append(recommendations.get(i).getItem().getID())
							.append(",");
			}
			SkShukuRecommand item = new SkShukuRecommand();
			
			SkUserDAO userFinder = new SkUserDAO();
			SkUser skUser = userFinder.findById(Integer.parseInt((String)user.getID()));
			item.setSkUser(skUser);
			item.getId().setSkUser(skUser);
			item.getId().setBookIdList(bookIdList.toString());
			SkShukuRecommandDAO saveRecommand = new SkShukuRecommandDAO();
			saveRecommand.save(item);
		}
		
		System.out.println("ok");
	}
}
